﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MyVoteMVC.Common;
using MyVoteMVC.Service;
using MyVoteMVC.Web.Extensions;
using System.Drawing;


namespace MyVoteMVC.Web.Controllers
{
    public class ImageController : Controller
    {
        public ActionResult ThumbnailByActivityID(string id)
        {
            //TODO:Add security checks
            using (IActivityService pService = Factory.GetInstance<IActivityService>())
            {
                var files = pService.GetActivityByIdWhenAdmin(id);
                var image = files.ActivityImage;

                if (image.Activity_Image != null)
                {
                    bool isBroken = false;
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Activity_Image))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }

                    if (isBroken)
                        return File("~/Content/NoPic.png", "image/png");
                    var desiredSize = new ImageSize { Width = 128, Height = 128 };
                    string contentType = "image/png";
                    var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                    using (var stream = new MemoryStream(image.Activity_Image))
                    {
                        return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                    }
                }
                //throw new HttpException(404, "File Not Found");
                return File("~/Content/NoPic.png", "image/png");
            }
        }

        public ActionResult ThumbnailByItemID(string id)
        {
            //TODO:Add security checks
            using (IItemService pService = Factory.GetInstance<IItemService>())
            {
                var files = pService.GetItems(id);
                var image = files.ItemImage;

                if (image.Item_Image != null)
                {
                    bool isBroken = false;
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Item_Image))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }

                    if (isBroken)
                        return File("~/Content/NoPic.png", "image/png");
                    var desiredSize = new ImageSize { Width = 128, Height = 128 };
                    string contentType = "image/png";
                    var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                    using (var stream = new MemoryStream(image.Item_Image))
                    {
                        return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                    }
                }
                //throw new HttpException(404, "File Not Found");
                return File("~/Content/NoPic.png", "image/png");
            }
        }

        public ActionResult ImageByItemID(string id)
        {
            //TODO:Add security checks
            using (IItemService pService = Factory.GetInstance<IItemService>())
            {
                var files = pService.GetItems(id);
                var image = files.ItemImage;

                if (image.Item_Image != null)
                {
                    bool isBroken = false;
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Item_Image))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }

                    if (isBroken)
                        return File("~/Content/NoPic.png", "image/png");
                    var desiredSize = new ImageSize { Width = 640, Height = 480 };
                    string contentType = "image/png";
                    var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                    using (var stream = new MemoryStream(image.Item_Image))
                    {
                        return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                    }
                }
                //throw new HttpException(404, "File Not Found");
                return File("~/Content/NoPic.png", "image/png");
            }
        }

        public ActionResult ThumbnailByProjectID(string id)
        {
            //TODO:Add security checks
            using (IProjectService pService = Factory.GetInstance<IProjectService>())
            {
                var files = pService.GetProjectById(id);
                var image = files.ProjectImage;
                bool isBroken = false;

                if (image.Project_Thumbnail != null)
                {
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Project_Thumbnail))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }
                    if (isBroken)
                        return File("~/Content/NoPic.png", "image/png");

                    string contentType = "image/png";
                    return File(image.Project_Thumbnail, contentType);
                }
                else

                if (image.Project_Image != null)
                {
                    
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Project_Image))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }

                    if (isBroken)
                      return File("~/Content/NoPic.png", "image/png");

                    var desiredSize = new ImageSize { Width = 128, Height = 128 };
                    string contentType = "image/png";
                    var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                    using (var stream = new MemoryStream(image.Project_Image))
                    {
                        return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                    }
                }
                return File("~/Content/NoPic.png", "image/png");
            }
        }

        public ActionResult ImageByProjectID(string id)
        {
            using (IProjectService pService = Factory.GetInstance<IProjectService>())
            {
                var files = pService.GetProjectById(id);
                var image = files.ProjectImage;
                if (image.Project_Image != null)
                {
                    bool isBroken = false;
                    try
                    {
                        using (var bmpstream = new MemoryStream(image.Project_Image))
                        {
                            Bitmap bmp = new Bitmap(bmpstream);
                            if (bmp == null)
                            {
                                //图片损坏
                                isBroken = true;
                            }
                        }
                    }
                    catch //(Exception ex)
                    {
                        //图片损坏
                        isBroken = true;
                    }

                    if (isBroken)
                        return File("~/Content/NoPic.png", "image/png");

                    //var desiredSize = new ImageSize { Width = 640, Height = 480 };
                    string contentType = "image/png";
                    //var thumbnailCreator = new ThumbnailCreator(new FitImageResizer());
                    return File(image.Project_Image, contentType);
                    //using (var stream = new MemoryStream(image.Project_Image))
                    //{
                    //    return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType);
                    //}
                }
                return File("~/Content/NoPic.png", "image/png");
            }
        }
    }
}
